package com.itheima.springboottlias.service.impl;

import Pojo.PageResult;
import Pojo.Student;
import Pojo.StudentQueryParam;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.springboottlias.anno.LogOperation;
import com.itheima.springboottlias.mapper.StudentMapper;
import com.itheima.springboottlias.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    //分页和条件查询
    @Override
    public PageResult page(StudentQueryParam studentQueryParam) {

        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        Page<Student> page = (Page<Student>) studentMapper.selectByCondition(studentQueryParam);
        for (Student student : page) {
            student.setUpdateTime(LocalDateTime.now());
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }
    //添加学员
    @LogOperation
    @Override
    public void addStudent(Student student) {
        studentMapper.insert(student);
    }
    //根据id查询学员
    @Override
    @LogOperation
    public Student findById(Integer id) {
        Student student = studentMapper.selectById(id);
        return student;
    }
    //修改学员
    @Override
    @LogOperation
    public void updateStudent(Student student) {
        studentMapper.update(student);
    }
    //修改学员违纪分数
    @Override
    @LogOperation
    public void updateScore(Integer id, Integer score) {
        studentMapper.updateScore(id, score);
    }
    //批量删除学员
    @Override
    @LogOperation
    public void remove(List<Integer> ids) {
        studentMapper.delete(ids);
    }

}
